package com.jkl.service.bsWorkflowLog;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkl.dao.bsWorkflowLog.BsWorkflowLogMapper;
import com.jkl.dao.person.BsPersonMapper;
import com.jkl.entity.bsWorkflowLog.BsWorkflowLog;
import com.jkl.entity.dept.BSDept;
import com.jkl.entity.person.BsPerson;
import com.jkl.service.bsWorkflowLog.inter.BsWorkflowLogServiceInterface;
import com.jkl.service.dept.BSDeptService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class BsWorkflowLogService implements BsWorkflowLogServiceInterface {

	@Autowired
	private BsWorkflowLogMapper bsWorkflowLogMapper;
	@Autowired
	private BSDeptService BSDeptService;
	@Autowired
	private BsPersonMapper BsPersonMapper;

	// 查询
	@Override
	public List<BsWorkflowLog> getSourceId(BsWorkflowLog bsWorkflowLog) {
		List<BsWorkflowLog> bsWorkflowLogList = bsWorkflowLogMapper.getSourceId(bsWorkflowLog);

		if (bsWorkflowLogList.size() != 0) {
			for (int x = 0; x < bsWorkflowLogList.size(); x++) {
				// 加上 上一级
				Integer row = bsWorkflowLogList.get(x).getPersonId();
				BsPerson BsPerson = BsPersonMapper.getPersonRow(row);
				Integer DeptId = BsPerson.getDeptId();
				List<BSDept> listBSDept = BSDeptService.getDeptObject(DeptId.toString());
				for (int i = 0; i < listBSDept.size(); i++) {
					if (listBSDept.get(i).getDeptName() != null) {
						if (bsWorkflowLogList.get(x).getBsDept().getParentId() != null) {
							if (bsWorkflowLogList.get(x).getBsDept().getParentId().equals(row)) {
								String name = listBSDept.get(i).getDeptName()
										+ bsWorkflowLogList.get(x).getBsDept().getDeptName();
								bsWorkflowLogList.get(x)
										.setBsDept(bsWorkflowLogList.get(x).getBsDept().setDeptName(name));
							} else {
								String deptRow = bsWorkflowLogList.get(x).getBsDept().getParentId().toString();
								List<BSDept> listBSDeptRow = BSDeptService.getDeptObject(deptRow);
								for (int j = 0; j < listBSDeptRow.size(); j++) {
									if (listBSDeptRow.size() != 0) {
										String name = listBSDeptRow.get(i).getDeptName()
												+ bsWorkflowLogList.get(x).getBsDept().getDeptName();
										bsWorkflowLogList.get(x)
												.setBsDept(bsWorkflowLogList.get(x).getBsDept().setDeptName(name));
									}
								}

							}
						}
					}
				}
			}
			return bsWorkflowLogList;
		} else {
			return null;
		}
	}

	@Override
	public int insertBsWorkflowLog(BsWorkflowLog bsWorkflowLog) {
		int i = bsWorkflowLogMapper.insert(bsWorkflowLog);
		if (i != 0) {
			return i;
		} else {
			return 0;
		}
	}

	@Override
	public PageInfo<BsWorkflowLog> getWorkflowLogList(BsWorkflowLog bsWorkflowLog) {
		PageHelper.startPage(bsWorkflowLog.getCurrentPage(), bsWorkflowLog.getPageSize());
		List<BsWorkflowLog> list = bsWorkflowLogMapper.getWorkflowLogList(bsWorkflowLog);
		PageInfo<BsWorkflowLog> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}
}
